
package chapter3;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.net.URL;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
/**
 * Բͼ
 * @author isource
 *
 */
public class Ex13 extends JApplet {
  public static void main(String s[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JApplet applet = new Ex13();
    applet.init();
    frame.getContentPane().add(applet);
    frame.pack();
    frame.setVisible(true);
  }
  
  public void init() {
    JPanel panel = new Ex13Panel();
    getContentPane().add(panel);
  }
}

class Ex13Panel extends JPanel{    
  BufferedImage image;
  
  public Ex13Panel() {
    setPreferredSize(new Dimension(400, 400));
    setBackground(Color.white);
    URL url = getClass().getClassLoader().getResource("images/stone.jpg");
    try {
      image = ImageIO.read(url);
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    int w = this.getWidth();
    int h = this.getHeight();
    Shape clip = new Ellipse2D.Double(w/5, h/5, 3*w/5, 3*h/5);
    g2.setClip(clip);
    g2.drawImage(image, 0, 0, w, h, this);
  }
}
